package com.bringway.fm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.fm.dao.AttDao;
import com.bringway.fm.service.AttService;
import com.bringway.framework.domain.SysBizAtt;
import com.bringway.framework.excel.ExcelSheet;
import com.bringway.global.dao.SysAttechmentDao;

import net.sf.json.JSONObject;

/*
* @author 作者 xuef
* @version 创建时间：2015年9月14日 上午10:52:07
* 类说明
*/
@Service
@Transactional
public class AttServiceImpl implements AttService{
	
	@Autowired
	private AttDao attDao;
	
	@Autowired
	private SysAttechmentDao sysAttechmentDao;

	@Override
	public List<Map<String, Object>>  queryAttList(JSONObject params) {
		return attDao.queryAttList(params);
	}

	@Override
	public int getAttCount(JSONObject params) {
		return attDao.getAttCount(params);
	}

	@Override
	public void saveAtt(JSONObject params) {
		String bizId = attDao.saveAtt(params);
		String atts = params.optString("atts");
		String[] att = atts.split(",");
		for (int i = 0; i < att.length; i++) {
			if("".equals(att[i]))continue;
			SysBizAtt bizAtt = new SysBizAtt();
			bizAtt.setBizId(bizId);
			bizAtt.setAttId(Long.decode(att[i]));
			sysAttechmentDao.createBizAtt(bizAtt);
		}
	}

	@Override
	public void deleteAtt(JSONObject params) {
		String bizId = params.optString("bizId");
		attDao.deleteAtt(params);
		sysAttechmentDao.delAttByBizId(bizId);
	}

	@Override
	public void topAtt(JSONObject params) {
		attDao.topAtt(params);
	}

	@Override
	public void unTopAtt(JSONObject params) {
		attDao.unTopAtt(params);
	}

	@Override
	public List<ExcelSheet> exportAttData(List<Map<String, Object>> attList) {
		 // 生成导入到EXECL的LIST
		List<ExcelSheet> sheetList = new ArrayList<ExcelSheet>();
		int headerCount = 3;
		// 点击汇总导出
		List<String[]> bodyList = new ArrayList<String[]>();
		for (int i = 0; i < attList.size(); i++) {
			String[] contents = new String[22];
			JSONObject obj = new JSONObject();
			obj = JSONObject.fromObject(attList.get(i));
			contents[0] = (i + 1)+"";//序号
			contents[1] = obj.optString("ID");//ID
			contents[2] = obj.optString("OPER_USER_ID");//OPER_USER_ID
			contents[3] = obj.optString("OPER_USER_NAME");//OPER_USER_NAME
			contents[4] = obj.optString("OPER_DATE");//OPER_DATE
			contents[4] = obj.optString("COMMENT");//COMMENT
			bodyList.add(contents);
		}
		ExcelSheet excelSheet = new ExcelSheet();
		excelSheet.setBodyList(bodyList);
		excelSheet.setSheetName("service层实现导出Excel");
		excelSheet.setFromRow(headerCount);
		sheetList.add(excelSheet);
		return sheetList;
	}
	
	
}
